﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using C1.Silverlight.Pdf;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public static class HouseMapLegend
    {
        
        public static void Draw(C1PdfDocument pdf, PDFPoint cursor, double borderWidth, double borderHeight, int columnCount)
        {
            if (null == pdf) return;
            if (null == cursor) return;
            if (borderHeight < 1) return;
            if (borderHeight < 1) return;
            if (columnCount < 1) return;

            // these variables determine all other widths of the legend, the heights are adjusted to fit
            const double legendItemsWidthPercent = .7;
            const double legendNameWidthPercent = .8;  // what portion of legendItemsWidth is for name
            const double legendSymbolWidthPercent = .1;  // what portion of legendItemsWidth is for symbol

            // draw border 
            Pen logoPen = new Pen(Colors.Black, 1.0);
            //pdf.DrawRectangle(logoPen, new Rect(cursor.x, cursor.y, borderWidth, borderHeight));

            double titleHeight = DrawTitle(pdf, cursor, borderWidth, borderHeight);

            // Build words to fit in the second column
            List<HouseMapLegendRow> fullItemList = new List<HouseMapLegendRow>();
            fullItemList.Add(new HouseMapLegendRowWithTextSymbol(pdf, HouseMapPens.districtLabelColor, "District Number", "7"));
            fullItemList.Add(new HouseMapLegendRowWithLineSymbol(pdf, HouseMapPens.districtBoundaryHighlightColor, HouseMapPens.districtBoundaryBorderColor, "District Boundary"));
            fullItemList.Add(new HouseMapLegendRowWithLineSymbol(pdf, HouseMapPens.countyBoundaryHighlightColor, HouseMapPens.countyBoundaryBorderColor, "County Boundary"));
            fullItemList.Add(new HouseMapLegendRowWithLineSymbol(pdf, HouseMapPens.interstateBoundaryBorderHighlightColor, HouseMapPens.interstateBoundaryBorderColor, "Interstate Highway"));
            fullItemList.Add(new HouseMapLegendRowWithLineSymbol(pdf, HouseMapPens.majorHighwayBoundaryBorderHighlightColor, HouseMapPens.majorHighwayBoundaryBorderColor, "Major Highway"));
            fullItemList.Add(new HouseMapLegendRowWithLineSymbol(pdf, HouseMapPens.shorelineBoundaryHighlightColor, HouseMapPens.shorelineBoundaryBorderColor, "Shoreline"));

            // attributes that apply to all columns
            double legendItemsOverallHeight = borderHeight - titleHeight;
            double legendItemsHeightPadding = 1;
            double legendItemsWidth = borderWidth * legendItemsWidthPercent / columnCount;  // how wide are the string columns, (note: need room for 2 string columns and 2 symbol columns
            double legendItemsOutsideMargin = (borderWidth * (1 - legendItemsWidthPercent) )/ 2;

            // layout is legendOutsideMargin, (legendItemsWidth) * columnCount, outsideMargin
            //   note: legendItemsWidth = (symbol, gapX, name, gapX)  

            double namesWidth = legendItemsWidth * legendNameWidthPercent;
            double symbolWidth = legendItemsWidth * legendSymbolWidthPercent; 
            double gapX = (legendItemsWidth - namesWidth - symbolWidth)/2;

            double offsetXSymbol = cursor.x + legendItemsOutsideMargin;
            double offsetXName = cursor.x + legendItemsOutsideMargin + symbolWidth + gapX;

            double legendItemHeight = 0.0;
            double legendItemFontSize = 0.0;

            // adjust the text size to fit into the space allowed
            int numberOfItemsInAColumn = fullItemList.Count / columnCount;
            int numberOfItemsTotal = numberOfItemsInAColumn * columnCount;
            if (numberOfItemsTotal != fullItemList.Count)
            {
                numberOfItemsInAColumn++;
            }

            int fullItemListIndex = 0;
            int fullItemListPrintIndex = 0;
            for (int col = 0; col < columnCount; col++)
            {
                // first go through the column list positioning things for drawing
                List<PDFString> columnItemList = new List<PDFString>();
                for (int i = 0; i < numberOfItemsInAColumn; i++)
                {
                    if (fullItemListIndex >= fullItemList.Count)
                    {
                        break;
                    }
                    columnItemList.Add(fullItemList[fullItemListIndex++].pdfString);
                }
                if (0 == col)
                {
                    legendItemsHeightPadding = PDFdoc.SetStringListFontSizeToFit(columnItemList, namesWidth, legendItemsOverallHeight, legendItemsHeightPadding);
                    // set the text size to match that just set
                    legendItemHeight = columnItemList[0].textBoxSize.Height;
                    legendItemFontSize = columnItemList[0].fontSize;
                }
                else
                {
                    offsetXSymbol += legendItemsWidth;
                    offsetXName += legendItemsWidth;
                }


                // now draw the column's legend items in the calculated position
                double offsetY = cursor.y + titleHeight + legendItemsHeightPadding;

                for (int i = 0; i < columnItemList.Count; i++)
                {
                    if (fullItemListPrintIndex < fullItemList.Count)
                    {
                        Rect symbolRect = new Rect(offsetXSymbol, offsetY, symbolWidth, legendItemHeight);
                        Rect nameRect = new Rect(offsetXName, offsetY, namesWidth, legendItemHeight);

                        fullItemList[fullItemListPrintIndex].pdfString.UpdateFontSize(legendItemFontSize);
                        fullItemList[fullItemListPrintIndex].DrawSymbol(symbolRect);
                        fullItemList[fullItemListPrintIndex].DrawName(nameRect);
                        offsetY += legendItemHeight + legendItemsHeightPadding;
                        fullItemListPrintIndex++;
                    }
                }
            }

        }

        private static double DrawTitle(C1PdfDocument pdf, PDFPoint legendCursor, double legendBorderWidth, double legendBorderHeight)
        {

            List<PDFString> stringList = new List<PDFString>();

            // Draw title to fit centered top
            double titlePadding = legendBorderHeight * .02;
            double titleWidth = legendBorderWidth * .4;
            double titleHeight = legendBorderHeight * .2;
            PDFString title = new PDFString(pdf, "Arial", 1, PdfFontStyle.Regular, Colors.Black, "Legend", 0);
            stringList.Add(title);
            titlePadding = PDFdoc.SetStringListFontSizeToFit(stringList, titleWidth, titleHeight, titlePadding);
            PDFPoint titleCursor = new PDFPoint(legendCursor.x, legendCursor.y);
            title.DrawCenteredX(new Rect(titleCursor.x, titleCursor.y, legendBorderWidth, titleHeight));

            return titleHeight;
        }

    }
}
